import { getCurrentInstance, ref } from 'vue';
import useUserStore from '@/store/modules/user';
import {type ElForm, type ElTable} from 'element-plus';

import { listContent, getContent, delContent, addContent, updateContent,qyListContent } from '@/api/manage/content';
export default (form:any,changeTemplate:any)=>{
	const { proxy } = getCurrentInstance() as any;
	const {roles} = useUserStore();
	const queryParams = ref({
		pageNum: 1,
		pageSize: 10,
		contentName: '',
		createBy: '',
		isStatus: '',
		productClassifyName:'',
		productlibraryId:[]

	});
	const contentList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref('');
	const queryRef = ref<InstanceType<typeof ElForm>>();

	// const rules = ref({});
	/** 查询产品库产品列表 */
	// function getList() {
	// 	loading.value = true;
	// 	listContent(queryParams.value).then((response: any) => {
	// 		contentList.value = response.data.list;
	// 		total.value = response.data.total;
	// 		loading.value = false;
	// 	});
	// }
	async function getList() {
		loading.value = true;
		let response = null;
		if(roles.includes('areaAdmin')){
			response = await qyListContent(queryParams.value);
		}else{
			response = await listContent(queryParams.value);
		}
		contentList.value = response.data.list;
		total.value = response.data.total;
		loading.value = false;
	}
	/** 提交按钮 */
	function handleSubmit() {
		proxy.$refs['contentRef'].validate((valid: boolean) => {
			if (valid) {
				// const params = { ...form.value, productclassifyId: form.value.productclassifyId.join() };
				if (form.value.id) {
					updateContent(form.value).then(response => {
						proxy.$modal.msgSuccess('修改成功');
						open.value = false;
						getList();
					});
				} else {
					addContent(form.value).then(response => {
						proxy.$modal.msgSuccess('新增成功');
						open.value = false;
						getList();
					});
				}
			}
		});
	}

	/** 删除按钮操作 */
	function handleDelete(row: any) {
		const idList = row.id || ids.value;
		proxy.$modal.confirm('是否确认删除产品库产品编号为"' + idList + '"的数据项？').then(function () {
			return delContent(idList);
		}).then(() => {
			getList();
			proxy.$modal.msgSuccess('删除成功');
		}).catch(() => { });
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('manage/content/exportByStream', {
			...queryParams.value
		}, `content_${new Date().getTime()}.xlsx`);
	}
	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		open.value = true;
		title.value = '添加产品库产品';
	}

	/** 修改按钮操作 */
	function handleUpdate(row: any) {
		reset();
		const id = row.id || ids.value;
		form.value = {...row};
		open.value = true;
		title.value = '修改产品库产品';
		changeTemplate(form.value.templateId);
	}
	/** 重置按钮操作 */
	function resetQuery() {
		proxy.resetForm(queryRef);
		handleQuery();
	}

	// 多选框选中数据
	function handleSelectionChange(selection: any) {
		ids.value = selection.map((item: any) => item.id);
		single.value = selection.length !== 1;
		multiple.value = !selection.length;
	}
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}
	function cancel() {
		open.value = false;
		changeTemplate();
		reset();
	}
	// 表单重置
	function reset() {
		form.value = {
			id: '',productclassifyId: '',
			contentName: '',contentFlag: '',restrictedPrintRecordcount: 0,labelContent:'1',dateFormat:'YYYY/MM/DD HH:mm',earlyWarning:'0',earlyWarningDate:0,
			productSort: 0,isStatus: '0',templateId: '',saveMethodContent: '常温储存',
			qualityPeriod: 10,
			thawTimeWord: '解冻时间',
			thawTime: 0,
			begintimeWord: '制作时间',
			endtimeWord: '到期时间',
			producerWord: '制作人',
			specsWord: '规格',
			batchingWord: '主要配料',
			expiryType:'自然日期', 
			dateType: '小时',
			pecsContent: '',
			batchingContent: '',
			qrcodeContent: '',
			bccodeContent: '',
			customWordOne: '',
			customWordTwo: ''
		};
		proxy.resetForm('contentRef');
	}
	return {
		handleSubmit,
		handleUpdate,
		handleAdd,
		handleExport,
		handleDelete,
		getList,
		resetQuery,
		handleSelectionChange,
		handleQuery,
		reset,
		cancel,
		queryRef,
		queryParams,
		contentList ,
		open ,
		loading,
		showSearch,
		ids,
		single,
		multiple ,
		total ,
		title,
	};
};